import { Button, Modal, message } from "antd";
import SelectOneOrderModal from "./SelectOneOrderModal";
import { useState } from "react";
import GoodsListModal from "./GoodsListModal";
import { useScanRegistrationStore } from "../../store";
import { fieldMap } from "../../fieldMap";

const ModalShowEnum = {
	/** 选择一条售后订单 */
	SelectOneOrderModal: 1,
	/** 选择商品 */
	GoodsSelectModal: 2
};
const AddGoods = props => {
	const { orderList, updateState } = useScanRegistrationStore()
	const [showModalType, setShowModalType] = useState("");
	const [currentSaleOrder, setCurrentSaleOrder] = useState('')

	function handleAddGoodsClick() {
		if (!orderList.length) {
			message.warning("暂无售后单无法添加商品");
			return;
		}

		if (orderList.length > 1) {
			setShowModalType(ModalShowEnum.SelectOneOrderModal);
			return;
		}
		
		setCurrentSaleOrder(orderList[0][fieldMap['售后单号']])
		setShowModalType(ModalShowEnum.GoodsSelectModal);
	}

  function handleGoodsChange (goodsList) {
		const index = orderList.findIndex(item  => item[fieldMap['售后单号']] === currentSaleOrder)
    const newGoodsList = goodsList.map(item => {
      // 不用传 次品数量 及 申请退货数量、唯一码、退款金额
      return {
				[fieldMap["售后商品id"]]: item.id,
				
				[fieldMap["系统商品图片"]]: item.image,
				[fieldMap["系统商品名称"]]: item.skuName,
				[fieldMap["系统商品编码"]]: item.skuCode,
				[fieldMap["系统款式编码"]]: item.spuCode,
				[fieldMap["系统颜色规格"]]: item.specs,
				[fieldMap["系统商品单价"]]: item.purchasePrice,
				[fieldMap["系统商品id"]]: item.id,

				[fieldMap["供应商商品图片"]]: item.supplierSkuImage,
				[fieldMap["供应商商品名称"]]: item.supplierSkuName,
				[fieldMap["供应商商品编码"]]: item.skuCode,
				[fieldMap["供应商商品编码id"]]: item.supplierSkuId,
				[fieldMap["供应商款式编码"]]: item.spuCode,
				[fieldMap["供应商颜色规格"]]: item.supplierSkuSpecs,
				[fieldMap["供应商商品单价"]]: item.purchasePrice,
				[fieldMap["供应商商品id"]]: item.supplierId,

				[fieldMap['供应商']]: item.goodsSkuMatches?.[0]?.supplierName,
				[fieldMap['供应商id']]: item.supplierId,

        [fieldMap['拆包数量']]: 0,
        [fieldMap['次品数量']]: 0,
        [fieldMap['申请退货数量']]: null,
        [fieldMap['退款金额']]: '',
        [fieldMap['商品唯一码']]: [],
				[fieldMap['是否是新增商品']]: true
      }
    })
		
    const goodsKey = fieldMap['售后商品列表']
    orderList[index][goodsKey] = orderList?.[index]?.[goodsKey]?.concat(newGoodsList)
    
    updateState({ orderList })
  }

	return (
		<>
			<Button type="primary" onClick={handleAddGoodsClick}>
				添加商品
			</Button>
			{
				{
					[ModalShowEnum.SelectOneOrderModal]: (
						<SelectOneOrderModal
							orderList={orderList}
							onChange={(val) => {
								setCurrentSaleOrder(val)
                setShowModalType(ModalShowEnum.GoodsSelectModal)
              }}
							onCancel={() => setShowModalType("")}
						/>
					),
					[ModalShowEnum.GoodsSelectModal]: (
						<GoodsListModal {...{
							handleCancel() {
								setShowModalType("")
							},
							onGoodsSkuListSelected(values) {},
							destroyOnClose: true,
							onSubmitSuccess: (data) => {
								handleGoodsChange(data)
								setShowModalType("")
							},
							// selectedIds: getCurrentGoodsInfo().selectedIds,
							// selectedSkuCodes: getCurrentGoodsInfo().selectedSkuCodes,
						}} />
					)
				}[showModalType]
			}
		</>
	);
};

export default AddGoods;
